package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.common.utils.HashMapUtils;
import com.j283_5.wisdom_park.mapper.*;
import com.j283_5.wisdom_park.pojo.*;
import com.j283_5.wisdom_park.service.CustomerDevelopmentService;
import com.j283_5.wisdom_park.vo.asset_management.InsertAnnRoomVo;
import com.j283_5.wisdom_park.vo.contract_management.ContractTableVo;
import com.j283_5.wisdom_park.vo.contract_management.CustomerNeedsVo;
import com.j283_5.wisdom_park.vo.contract_management.CustomerVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户发展表 服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class CustomerDevelopmentServiceImpl implements CustomerDevelopmentService {

    @Resource
    private CustomerDevelopmentMapper mapper;
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private TenementMapper tenementMapper;
    @Resource
    private CustomerNeedsMapper customerNeedsMapper;
    @Resource
    private PersonMapper personMapper;
    @Resource
    private RoomMapper roomMapper;
    /**
     * 多条件查询客户信息
     * @param customerVo
     * @return
     */
    @Override
    public Map<String,Object> findCustomer(CustomerVo customerVo, Integer page, Integer limit) {
        Integer start=null;
        //总条数
        Long countRows=null;
        List<CustomerVo> list=null;
        if (page!=null&&limit!=null){
            start=(page-1)*limit;
        }
        countRows=mapper.findCustomerCount(customerVo);
        //如果总条数大于0  才去查询数据
        if(countRows!=null && countRows>0){
            list=mapper.findCustomer(customerVo,start,limit);
            return new HashMapUtils()
                    .addData("countRows",countRows)
                    .addData("listCustomer",list)
                    .getMap();
        }else{
            return new HashMapUtils().getMap();
        }
    }

    /**
     * 根据客户id查询客户基本信息
     * @param cus_id
     * @return
     */
    @Override
    public Map<String, Object> findCustomerById(Integer cus_id) {
//        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
//        adminQueryWrapper.select("admin_id,admin_name");
//        List<Admin> adminList = adminMapper.selectList(adminQueryWrapper);
//
//        QueryWrapper<CustomerDevelopment> customerDevelopmentQueryWrapper = new QueryWrapper<>();
//        customerDevelopmentQueryWrapper.select("per_grade,follow_state");
//        List<CustomerDevelopment> customerDevelopmentList = mapper.selectList(customerDevelopmentQueryWrapper);
//
//        QueryWrapper<Tenement> tenementQueryWrapper = new QueryWrapper<>();
//        tenementQueryWrapper.select("ten_Serialint,ten_Id");
//        List<Tenement> tenements = tenementMapper.selectList(tenementQueryWrapper);



        List<CustomerVo> list = mapper.findCustomerById(cus_id);
        List<CustomerNeedsVo> customerNeedList = mapper.findCustomerNeedById(cus_id);


        return new HashMapUtils().addData("CustomerInfoList",list)
                                .addData("customerNeedList",customerNeedList)
                                .getMap();
    }

    /**
     * 更新
     * @param customerNeedsVo
     * @param customerVo
     * @return
     */
    @Override
    public ResultData UpdateCustomer(CustomerNeedsVo customerNeedsVo, CustomerVo customerVo) {
        UpdateWrapper<CustomerDevelopment> customerDevelopmentUpdateWrapper = new UpdateWrapper<>();
        customerDevelopmentUpdateWrapper.set("per_grade",customerVo.getPerGrade());
        customerDevelopmentUpdateWrapper.set("follow_state",customerVo.getFollowState());
        customerDevelopmentUpdateWrapper.set("admin_id",customerVo.getAdminId());
        customerDevelopmentUpdateWrapper.eq("cus_id",customerVo.getCusId());
        int update = mapper.update(null, customerDevelopmentUpdateWrapper);

        UpdateWrapper<CustomerNeeds> customerNeedsUpdateWrapper = new UpdateWrapper<>();
        customerNeedsUpdateWrapper.eq("cus_need_id",customerVo.getCusNeedId())
                                    .set("cus_need_minSize",customerNeedsVo.getCusNeedMinsize())
                                    .set("cus_need_maxSize",customerNeedsVo.getCusNeedMaxsize())
                                    .set("cus_need_minMoney",customerNeedsVo.getCusNeedMinmoney())
                                    .set("cus_need_maxMoney",customerNeedsVo.getCusNeedMinmoney())
                                    .set("cus_need_purpose",customerNeedsVo.getCusNeedPurpose())
                                    .set("ten_id",customerNeedsVo.getTenId())
                                    .set("room_id",customerNeedsVo.getRoom_id());
        int update1 = customerNeedsMapper.update(null, customerNeedsUpdateWrapper);

        if (update>0&&update1>0){
            return ResultData.ok();
        }

        return ResultData.error();
    }

    /**
     * 删除客户
     * @return
     */
    @Override
    public ResultData DeleteCustomer(List<Integer> listCustomer) {
        UpdateWrapper<CustomerDevelopment> customerDevelopmentUpdateWrapper = new UpdateWrapper<>();
        customerDevelopmentUpdateWrapper.set("cus_delete",1);
        customerDevelopmentUpdateWrapper.in("cus_id",listCustomer);
        int i = mapper.update(null, customerDevelopmentUpdateWrapper);
        if(i > 0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.error().message("删除失败！");
    }

    /**
     * 根据客户id查询跟进情况
     * @return
     */
    @Override
    public ResultData Follow_situation(Integer cus_id) {
        List<CustomerVo> list = mapper.Follow_situation(cus_id);

        return  ResultData.ok().data(list);
    }

    /**
     * 新增客户
     * @param customerNeedsVo
     * @param customerVo
     * @return
     */
    @Override
    public ResultData InsertCustomer(CustomerNeedsVo customerNeedsVo, CustomerVo customerVo) {
        Admin admin=new Admin();
        admin.setAdminName(customerVo.getAdminName());
        adminMapper.insert(admin);
        Integer adminId = admin.getAdminId();

        Person person=new Person();
        person.setPersonName(customerVo.getPersonName());
        person.setPersonPhone(customerVo.getPersonPhone());
        personMapper.insert(person);

        CustomerNeeds customerNeeds=new CustomerNeeds();
        customerNeeds.setCusNeedMaxmoney(customerNeedsVo.getCusNeedMaxmoney());
        customerNeeds.setCusNeedMaxsize(customerNeedsVo.getCusNeedMaxsize());
        customerNeeds.setCusNeedMinmoney(customerNeedsVo.getCusNeedMinmoney());
        customerNeeds.setCusNeedMinsize(customerNeedsVo.getCusNeedMinsize());
        customerNeeds.setCusNeedPurpose(customerNeedsVo.getCusNeedPurpose());
        customerNeeds.setRoomId(customerNeedsVo.getRoom_id());

        CustomerDevelopment customerDevelopment=new CustomerDevelopment();
        customerDevelopment.setPerGrade(customerVo.getPerGrade());
        customerDevelopment.setFollowState(customerVo.getFollowState());
        customerDevelopment.setAdminId(adminId);
        int insert = mapper.insert(customerDevelopment);

        if (insert>0){
            return ResultData.ok().message("插入成功");
        }else {
            return ResultData.error().message("插入失败");
        }

    }




}
